package com.gexin.controller.system.user;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gexin.common.utils.StringUtil;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.system.AdminRes;
import com.gexin.model.system.RoleKit;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;
/**
 * 权限管理
 * @author 肖得明
 *  用户资源
 */
@ControllerBind(controllerKey = "/system/role/res", viewPath = "WEB-INF/system/admin/")
public class AdminResController extends SystemBaseController {

	//首页  查询所有的 一级菜单
	public void index(){
		render("res_list.html");
	}
	
	/**
	 * 查询
	 */
	public void query() {
		Map<Object, Object> dataMap = new HashMap<Object, Object>();
		Page<AdminRes>  page =  AdminRes.dao.paginate(getpage(), getpageSize());
		for (AdminRes r : page.getList()) {
			r.put("isShow",false);
			List<AdminRes> twoLevel=AdminRes.dao.getResByPid(r.getId());
			r.put("twoLevel", twoLevel);
			for (AdminRes two : twoLevel) {
				two.put("threeLevel",AdminRes.dao.getResByPid(two.getId()));
				two.put("isShow",false);
			}
		}
		dataMap.put("pageData", page);
		renderJson(dataMap);
	}
	
	
	/**
	 * 删除
	 */
	public void delete(){
		int pid =getParaToInt();
		if(AdminRes.dao.getResByPid(pid).size()>0){
			fail("下面有子类，请先删除！");
			return ;
		}
		AdminRes.dao.deleteById(getPara());
		RoleKit.init();
		success();

	}
	//获取详细信息
	public void info(){
		renderJson(AdminRes.dao.findById(getPara()));
	}
	
	/**
	 * 添加或者保存
	 */
	public void addsave(){
		AdminRes res  =   getModel(AdminRes.class,"res");
		if(res.getId()==null || res.getId()==0){
			if(res.getType() ==null || res.getType()!=2){
				res.setCssTag(StringUtil.generationCode());
			}
			res.save();
		}else{
			//修改  要清理全部的权限缓存
			RoleKit.clear_Role_All();
			res.update();
		}
		RoleKit.init();
		success();
	}
	
	
	//跳转图标选择页面
	public void selectIcon() {
		render("admin_select_icons.html");
	}
	
}


